//
//  InformationViewController.m
//  Dear
//
//  Created by 陈俊俊 on 15/7/11.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "InformationViewController.h"
#import "SexCell.h"
#import "DDetailViewController.h"
#import "SexModel.h"
#import "CommentModel.h"
//按钮的tag
typedef enum {
    WhereTag = 3001,
    LookTag ,
    SexesTag,
    EmotionalTag,
    RelationTag
} Tag;

@interface InformationViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * _dataArr;
    UITableView *_tableView;
    BOOL _isRefresh;
}
@end
@implementation InformationViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self uiConfig];
    [self createRefresh];
    [self getDataFromServer];
    
}
- (void)initData{
    _dataArr = [[NSMutableArray alloc]init];

}
- (void)getDataFromServer {
    
    //2 资讯
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"type":@"2"};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpInformation];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSLog(@"%@",UUIDStr);
            if (_dataArr.count > 0) {
                [_dataArr removeAllObjects];
            }
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0) {
                NSArray *itemArr = dict[@"item"];
                for (NSDictionary *itemDict in itemArr) {
                    SexModel *model = [[SexModel alloc]init];
                    [model setValuesForKeysWithDictionary:itemDict];
                    
                    NSMutableArray *comments = [[NSMutableArray alloc]init];
                    NSArray *commentArr = itemDict[@"commentItem"];
                    for (NSDictionary *commentDict in commentArr) {
                        CommentModel *commentModel = [[CommentModel alloc]init];
                        [commentModel setValuesForKeysWithDictionary:commentDict];
                        NSDictionary *userDict = commentDict[@"userItem"];
                        commentModel.userCode = userDict[@"userCode"];
                        commentModel.userName = userDict[@"userName"];
                        commentModel.userRoles = userDict[@"userRoles"];
                        commentModel.picture = userDict[@"picture"];
                        [comments addObject:commentModel];
                    }
                    model.commentItem = comments;
                    [_dataArr addObject:model];
                }
                [_tableView reloadData];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
        [self endRefresh];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];
}
- (void)createRefresh{
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (_isRefresh) {
            return ;
        }
        _isRefresh = YES;
        [self getDataFromServer];
    }];
    _tableView.header.lastUpdatedTimeKey = InfoUpdateTime;
}
- (void)endRefresh
{
    _isRefresh = NO;
    [_tableView.header endRefreshing];
}


- (void)uiConfig {
    [self createTitleViewWith:@"资讯"];
    
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64 - 49))];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    [button setTitle:@"查看更多" forState:(UIControlStateNormal)];
//    button.layer.masksToBounds = YES;
//    button.layer.borderWidth = 0.5;
//    button.layer.borderColor = RGBA(231, 231, 231, 1).CGColor;
//    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//    button.frame = CGRectMake(0, 0, ScreenSize.width, 44);
//    _tableView.tableFooterView = button;
    [self.view addSubview:_tableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellID = @"SexCell";
    SexCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"SexCell" owner:self options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataArr.count > 0) {
        SexModel *sexModel = _dataArr[indexPath.row];
        cell.sexModel = sexModel;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDetailViewController *dvc  = [[DDetailViewController alloc]init];
    if (_dataArr.count > 0) {
        SexModel *model = _dataArr[indexPath.row];
        dvc.model = model;
    }
    dvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:dvc animated:YES];
}

@end
